﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.IO;

namespace AtletIS.Shared.Helpers
{
    public static class NumberScoreBoardHelper
    {
        private static Encoding _encoding = Encoding.GetEncoding(1250);
        
        #region Public methods

        public static void ClearDataPart(int portNumber)
        {
            try
            {
                string portName = string.Format("COM{0}", portNumber);
                string data = "A             ";

                using (SerialPort port = new SerialPort(portName, 9600, Parity.None, 8, StopBits.One))
                {
                    port.Encoding = _encoding;

                    string s = Paket(data);

                    port.Open();
                    port.WriteLine(s);
                    port.Close();
                }
            }
            catch (Exception ex) { Console.WriteLine("ClearDataPart" + ex.Message); }
        }

        public static void SendDataPart(int portNumber, string startNumber, string attempt, string performance)
        {
            try
            {
                string portName = string.Format("COM{0}", portNumber);

                startNumber = startNumber.PadRight(3, ' ');
                performance = performance.PadLeft(4, ' ');
                performance = performance.Substring(0, 2) + "0" + performance.Substring(2, 2);

                string data = string.Format("A{0}{1}0000{2}", startNumber, attempt, performance);
                
                using (SerialPort port = new SerialPort(portName, 9600, Parity.None, 8, StopBits.One))
                {
                    port.Encoding = _encoding;
                    
                    string s = Paket(data);

                    port.Open();
                    port.WriteLine(s);
                    port.Close();
                }
            }
            catch (Exception ex) { Console.WriteLine("SendDataPart" + ex.Message); }
        }

        public static void SendTimePart(int portNumber, byte hours, byte minutes)
        {
            try
            {
                byte seconds = 0;
                string portName = string.Format("COM{0}", portNumber);

                //StreamWriter sw = new StreamWriter("board.txt", true);
                //sw.Write(string.Format("T[{0:00}{1:00}{2:00}]", hours, minutes, seconds));

                using (SerialPort port = new SerialPort(portName, 9600, Parity.None, 8, StopBits.One))
                {
                    port.Encoding = _encoding;
                    
                    hours = Cpack(hours);
                    minutes = Cpack(minutes);
                    seconds = Cpack(seconds);

                    string s  = _encoding.GetString(new byte[] { 116, hours, minutes, seconds });
                    s = Paket(s);

                    //sw.WriteLine(s);
                    //Console.WriteLine("[" + s + "]");

                    port.Open();
                    port.Write(s);
                    port.Close();
                    port.Dispose();
                }

                //sw.Close();
                //sw.Dispose();
            }
            catch (Exception ex) { Console.WriteLine("SendTimePart" + ex.Message); }
        }

        #endregion

        #region Private methods

        /// <summary>
        /// Vytvorenie paketu pre tabuľu.
        /// </summary>
        private static string Paket(string s)
        {
            byte chkSum = 0;

            string str = _encoding.GetString(new byte[] { 2, 100 }) + s + _encoding.GetString(new byte[] { 3 });

            foreach (byte chrByte in _encoding.GetBytes(str))
            {
                chkSum = Convert.ToByte(chkSum ^ chrByte);
            }

            return str + _encoding.GetString(new byte[] { chkSum });
        }

        /// <summary>
        /// Prevod čísla z 10 na 16.
        /// </summary>
        private static byte Cpack(byte c)
        {
            return Convert.ToByte(((c / 10) + 5) * 16 + (c % 10) + 5);
        }

        #endregion

    }
}
